clear all
*set type double, permanently
set matsize 3000

* set file path
gl dat ""
gl do ""
gl text ""
gl texg ""

cap pr drop max
pr de max
	ren `1' `1'_
	bys id: egen `1'=max(`1'_)
	drop `1'_
end


use "$dat/ZEMIS_final.dta", clear


*modify some variables to be able to use in regressions
foreach x in ethn_cd reli_cd spr_cd country {
replace `x'=0 if `x'==-1 // can't do fixed effects with negative values
}

g fcenter2=fcenter // to be able to identify and indicate the interaction automatically in table
g cantnum2=cantnum
g year2=year-1998 if !mi(year)


*create indicator for how many years individuals are observed during first 3-5 years 
forvalues x=3/5 {
bys id (year): egen ind_cov_`x'=count(id) if yearN<=`x'
}

tab ind_cov_5 if yearN==5 // 99,7 observed all 5 years

******************
***   globals   ** 
******************

* sample restriction in main analysis
* remove if not working age, assigned to requested canton, didn't get F permit, re-enter
gl restb  age_arrive>=18 & age_arrive<=65 & cantreq!=1 & getF==1 & gap_day!=1
gl rest $restb  & ind_cov_5==5 


*save sample to permutation test
preserve
g ind=1 if $rest 
keep if ind==1
keep share_conat_cow share_coeth_cow share_colan_cow id year ind  conat_cant_cow_pre coeth_cant_cow_pre colan_cant_cow_pre
save "$dat/permutation_indicator.dta", replace
restore

*check that panel is balanced
forvalues i=1/5 {
sum year if yearN==`i' & $rest & !mi(lnmatch) // 8593
}
sum year if $rest  & !mi(lnmatch) // 42965 (43675 without lnmatch restriction)
sum year if $rest 


gl addbase star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3)  fragment booktabs nonotes    label 
gl add $addbase stat(r2_a N, fmt(3 0) labels("Adjusted \$ R^2$" Observations)) 
gl indicate "Country FE=*.country" "Arrival year FE= *.farrive"  "Center FE =*.fcenter"  "Canton FE = *.cantnum" 
gl indicate2 "Center*Arrival year FE = *.fcenter2" "Individual covariates=age_arrive" "Individual covariates FE = *.ethn_cd" "Canton covariates = unemp_ass" "Canton*Country FE = *.cantnum2" "Canton trends= *.cantnum#c.year2" 
gl cont_ind_base age_arrive  age_arrive2 woman fmarried  with_child famsize french_int i.german_info i.french_info
gl cont_ind  $cont_ind_base  i.ethn_cd i.spr_cd  i.reli_cd
gl cont_cant  unemp_ass lnmig
gl cont $cont_ind $cont_cant  i.country  i.farrive i.cantnum i.fcenter farrive#fcenter2  
gl cont_placebo  $cont_ind   unemp_ass i.country   i.farrive i.cantnum   i.fcenter farrive#fcenter2
gl ylist emp emp90 high1 high2
gl clust cantnum


****************
****  LABEL ****
***************
qui do "$do/industry.do"

loc l la var
`l' famsize "Family size"
`l' with_child "Arrive with child"
`l' age Age
`l' age_arrive "Age at arrival"
`l' farrive "Arrival year"
`l' woman Woman
`l' fmarried Married
`l' wait_perm "Wait time (days)"
`l' getF "Get F-permit"
`l' emp Employed
`l' match "Co-nationals"
`l' match_all "Co-nationals, all ages"
`l' match_current "Co-nationals, arrival year"
`l' lnmatch_current "ln(co-nationals), arrival year"
`l' lnmatch "ln(co-nationals)"
`l' lnematch "ln(co-ethnic)"
`l' lnlmatch "ln(co-linguistic)"
`l' lnmatch_all "ln(co-nationals), all ages"
`l' mig Immigrants
`l' lnmig "ln(immigrants)"
`l' unemp Unemployment
`l' french "French speaking country"
`l' age_arrive2 "Age at arrival$^{2}$"
`l' unemp_ass "Unemployment rate"
`l' lnwait_perm "ln(wait time)"
`l' share_ethn "Share co-nationals, same ethnicity"
`l' share_spr "Share co-nationals, same language"
`l' emp90 "Employed $\geq$ 90 days"
`l' high1 "High skilled (work task)"
`l' high2 "High skilled (sector)"
`l' emp Employed
`l' bur_id "Business ID"
`l' cantass "Assigned canton"
`l' nowunsch_info "No request specific cantom"
`l' nocomcant_info "Caseworker no request specific cantom"
`l' cantreq "Approved request specific canton"
`l' ethn_cd "Ethnicity"
`l' spr_cd "Language"
`l' reli_cd "Religion"
`l' farrive "First arrival year"
`l' fcase_begin "First case begin"
`l' fcenter "First application center"
`l' fmarried "Married arrival year"
`l' getF "Get F-permit"
`l' move_later "Not living in assignment canton"
`l' french_cant "French speaking canton"
`l' french_col "French colony"
`l' french_int "French speaking canton*country"
`l' industry1 "Work task"
`l' industry2 Sector
`l' cantnum "Assigned canton"
`l' multiple_cant_tot "Multiple assigned cantons"
`l' german_info "German mentioned, assignment info "
`l' french_info "French mention, assignment info"
`l' yearS "Total years observed"
`l' yearN "Current year observed"


***********************************
*  Table S1: Summary stat table  **
***********************************

eststo clear
estpost sum age_arrive  woman  fmarried with_child famsize farrive  wait_perm   emp  emp90 high1 high2 match lnmatch lnematch lnlmatch  unemp  mig  share_spr share_ethn if $rest  & !mi(lnmatch)
esttab, cells("mean(fmt(2)) sd(fmt(2)) min(fmt(a3)) max(fmt(a3))" )  nonumb
esttab using "$text/sumstat.tex", replace cells("mean(fmt(2)) sd(fmt(2)) min(fmt(a3)) max(fmt(a3))") title() fragment nonotes nogaps label nomtitle nonumb varlabels(,  blist(match " \hline Canton covariates \\ \hline "))


************************************
** Table S2: Table top countries  **
************************************ 

qui do "$do/label_country.do" 
la val country country_lab

cap drop obs sum_all sum_country* top 
g obs=1
egen sum_all=count(obs) if $rest & yearN==1
bys country: egen sum_country=count(obs) if $rest & yearN==1 
bys country: egen sum_country_all=max(sum_country) if $rest 
g share= sum_country/sum_all
replace share=share*100

gsort -share country

g top=sum(country!=country[_n-1]) if !mi(share)
tab country if top<=15

labmask top, values(country) decode // transfer labels
tab top

eststo clear
eststo: estpost tabstat sum_country  if $rest & yearN==1 & top<=15, by(top) statistics(mean) columns(statistics) nototal 
eststo: estpost tabstat share  if $rest & yearN==1 & top<=15, by(top) statistics(mean) columns(statistics) nototal 

esttab using "$text/sumstat_countries.tex", replace cells("mean(fmt(0))") collabels(none) fragment nonum  mtitles("Number of individuals" "\% of sample") 
drop share obs sum_all sum_country* top



*******************************************************
*** calculate needed statistics (reported in paper) ***
*******************************************************

* employment rate by gender
sum emp if woman==1 & $rest  // 8,5 %
sum emp if woman==0 & $rest  // 24 %

*share moving
sum move_later if $rest & yearN==5 // 4,5 %

* share employed 3d year
sum emp if $rest & yearN==3 //0.171

* average share co-national co-workers
sum share_conat_cow if $rest & uniq_firm==1 & conat_cant_cow_pre>=2 & !mi(conat_cant_cow_pre) // 0.36
 
*************************************************************************
** Fig 3 and s1: calculate share co-workers belonging to worker group  **
************************************************************************* 

** Data exported to R ***

*figure s1
foreach x in conat  coeth colan {
cap mat drop x x2
tab uniq_firm if `x'_cant_cow_pre>=2 & uniq_firm==1 & $rest , matcell(x) 
tab uniq_firm if `x'_cow_pre==0 & uniq_firm==1  & `x'_cant_cow_pre>=2 & !mi(`x'_cant_cow_pre) & $rest  , matcell(x2)   
mat `x'=x2[1,1] / x[1,1] 
tab uniq_firm if `x'_cow_pre==1 & uniq_firm==1  & `x'_cant_cow_pre>=2  & !mi(`x'_cant_cow_pre) & $rest , matcell(x2) 
mat `x'= nullmat(`x') \ x2[1,1] / x[1,1]
tab uniq_firm if `x'_cow_pre==2 & uniq_firm==1 & `x'_cant_cow_pre>=2 & !mi(`x'_cant_cow_pre)  & $rest , matcell(x2) 
mat `x'= nullmat(`x') \ x2[1,1] / x[1,1]
tab uniq_firm if `x'_cow_pre>2  & uniq_firm==1 & `x'_cant_cow_pre>=2 & !mi(`x'_cant_cow_pre)  & $rest , matcell(x2) 
mat `x'= nullmat(`x') \ x2[1,1] / x[1,1]
matlist `x'
}

mat A = conat, coeth, colan
mat coln A = conat coeth colan
mat rown A =  "0" "1" "2" "3+" 
matlist A
mat2txt , matrix(A) saving("$texg/num_coworkers.txt") replace


* figure 3
loc j=0
foreach x in conat  coeth colan {
loc ++ j
display `j'
cap mat drop `x'
ci means share_`x'_cow if $rest  & uniq_firm==1 & `x'_cant_cow_pre>=2 & !mi(`x'_cant_cow_pre) 
mat `x' = r(mean), r(lb), r(ub), `j'
ci means share_`x'_cow if $rest  & uniq_firm==1 & `x'_cant_cow_pre>=10 & !mi(`x'_cant_cow_pre)  
mat `x' = nullmat(`x') \ r(mean),  r(lb), r(ub), `j'
ci means share_`x'_emp if $rest  & uniq_emp==1  & `x'_cant_emp_pre>=2 & !mi(`x'_cant_emp_pre)  
mat `x' = nullmat(`x') \r(mean),  r(lb), r(ub), `j'
ci means share_`x'_sec if $rest  & uniq_indu==1  & `x'_cant_sec_pre>=2 & !mi(`x'_cant_sec_pre)  
mat `x' = nullmat(`x') \ r(mean),  r(lb), r(ub), `j'
}

mat sorder = 1 \ 2 \ 3 \ 4
mat B = conat, sorder \ coeth, sorder \ colan, sorder
mat coln B = value  lb ub group sorder
loc l "Co-workers" "Co-workers (10+ employees)" "Employed refugees in canton" "Employed refugees in sector" 
mat rown B = "`l'" "`l'"  "`l'" 
matlist B
mat2txt , matrix(B) saving("$texg/share_coworkers.txt") replace




*****************************************
*** Table S3: check balance wait time ***
*****************************************

loc var lnwait_perm  
eststo clear
foreach x in  lnmatch  lnematch lnlmatch  {
eststo: reg `x' `var' i.country i.farrive if $rest & yearN==1  , cl($clust) 
eststo: reg `x' `var' i.country  i.farrive i.fcenter  i.cantnum    if $rest & yearN==1 , cl($clust) 
}

esttab, keep(`var')  stat( N, fmt( 0 ) labels( Observations)) star(* 0.10 ** 0.05 *** 0.01)
esttab using "$text/balance.tex", replace $add keep(`var')  indicate( "$indicate" , l(\checkmark ) )



********************************************
** S4: interaction effect (not marginal) ***
********************************************

eststo clear
loc x lnmatch
foreach y in  $ylist {
eststo: reg `y' `x' c.`x'#yearN i.yearN $cont if $rest  , cl( $clust)
}
esttab, keep( `x' *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 
esttab using "$text/`x'_nonmarg.tex", replace keep(`x' 2.yearN#c.`x' 3.yearN#c.`x' 4.yearN#c.`x' 5.yearN#c.`x')  $add  coef(`x' "`:variable label `x''" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 


***********************************
*** Table S5- S7: Main results  ***
***********************************

** display test version 
eststo clear
loc x lnmatch 
foreach y in $ylist {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont  if $rest  , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 


** table main results
loc t1 1 //marginal
loc t2 1 //table
foreach x in  lnmatch lnematch lnlmatch  {
eststo clear
foreach y in  $ylist  {
cap mat drop  `y'_`x'
eststo `y'_`x': reg `y' c.`x'#yearN i.yearN $cont if $rest  , cl($clust)
mat t=r(table)
mat `y'_`x'=t[1,1], t[5,1], t[6,1]\t[1,2], t[5,2], t[6,2]\t[1,3], t[5,3], t[6,3]\t[1,4], t[5,4], t[6,4]\t[1,5], t[5,5], t[6,5]
cap mat rownames `y'_`x' = "1" "2" "3" "4" "5" 
cap mat colnames `y'_`x' = val cl cu
if "`y'"=="emp" & `t1'==1 {
foreach p in  25 75  {
cap mat drop pt`p'_`y'_`x'
cap mat drop t
forvalues z=1/5 {
margins, at((p`p') `x' yearN=`z'  ) 
mat t=r(table)
mat pt`p'_`y'_`x'=nullmat(pt`p'_`y'_`x') \t[1,1], t[5,1], t[6,1]
}
}
}
}
if `t2'==1 {
esttab using "$text/`x'.tex", replace keep(*.yearN#c.`x')  $add  coef(1.yearN#c.`x' "`:variable label `x'', year 1" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 
eststo clear
}
}

***** Data exported to R ****

foreach y in lnmatch lnematch lnlmatch  {
loc x emp 
matlist `x'_`y'
mat2txt , matrix(`x'_`y') saving("$texg/`x'_`y'.txt") replace
matlist pt25_`x'_`y'
matlist pt75_`x'_`y'
}


mat num= (1\2\3\4\5)
mat p25 = (25\25\25\25\25)
mat p75 = (75\75\75\75\75)
foreach y in lnmatch lnematch lnlmatch  {
loc x  emp 
cap mat drop `x'
mat `x' = num, p25, pt25_`x'_`y' \ num, p75, pt75_`x'_`y'
mat coln `x' = num var val cl cu
mat2txt , matrix(`x') saving("$texg/`x'_`y'_2575.txt") replace
matlist `x'
}


*****************************************************************************
*** Check if signficantly different effect for ethnicity/language/country ***
*****************************************************************************

reg emp  c.lnmatch#yearN i.yearN   $cont  if $rest , cl($clust)   
lincom c.lnmatch#3.yearN // p-value 0.001, ci  .013 -  .044
lincom c.lnmatch#3.yearN *0.09531  // ln(1.1)= 0.09531, p-value 0.001, ci  .0012 - .0042


fvset base 9 spr_cd
fvset base 13 ethn_cd
reg emp  c.lnmatch#yearN i.yearN   $cont  if $rest   
lincom c.lnmatch#3.yearN 
estimates store reg1
reg emp  c.lnematch#yearN i.yearN  $cont if $rest   
estimates store reg2
reg emp  c.lnlmatch#yearN i.yearN  $cont  if $rest  
estimates store reg3

suest reg1  reg2 reg3 , cl($clust)
test [reg1_mean]c.lnmatch#1.yearN  = [reg2_mean]c.lnematch#1.yearN = [reg3_mean]c.lnlmatch#1.yearN // p-value 0.00
test [reg1_mean]c.lnmatch#2.yearN  = [reg2_mean]c.lnematch#2.yearN = [reg3_mean]c.lnlmatch#2.yearN // p-value 0.23
test [reg1_mean]c.lnmatch#3.yearN  = [reg2_mean]c.lnematch#3.yearN = [reg3_mean]c.lnlmatch#3.yearN // p-value 0.74
test [reg1_mean]c.lnmatch#4.yearN  = [reg2_mean]c.lnematch#4.yearN = [reg3_mean]c.lnlmatch#4.yearN // p-value 0.41
test [reg1_mean]c.lnmatch#5.yearN  = [reg2_mean]c.lnematch#5.yearN = [reg3_mean]c.lnlmatch#5.yearN // p-value 0.63

**************************************
*** Table S8:  total average effect **
**************************************

eststo clear
loc x lnmatch
foreach y in $ylist {
eststo: reg `y' `x' i.yearN $cont if $rest , cl($clust)
sum `y' if $rest 
estadd scalar mval=r(mean)
}
esttab,  keep(`x') star(* 0.10 ** 0.05 *** 0.01)
esttab using "$text/`x'_total.tex", replace keep(`x')  $addbase  stat(mval r2_a N, fmt(3 3 0) labels("Mean value" "Adjusted \$R^2$" Observations))  

*************************************************************
*** Table S9:  drop cases with any info on canton request ***
*************************************************************

eststo clear
loc x lnmatch
foreach y in  $ylist {
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest  & nowunsch_info==1 & nocomcant_info==1, cl( $clust)
}
esttab, keep( *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 
esttab using "$text/`x'_nowunsch.tex", replace keep(*.yearN#c.`x')  $add  coef(1.yearN#c.`x' "`:variable label `x'', year 1" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 



****************************************
** Table s10: Add covariates stepwise **
****************************************

loc c   i.country i.fcenter  i.farrive i.cantnum 
loc x lnmatch 
loc con $rest , cl( $clust) 
loc y  emp
eststo clear
eststo: reg `y' c.`x'#yearN i.yearN  `c'   if  `con'
eststo: reg `y' c.`x'#yearN i.yearN  `c'  farrive#fcenter2 if  `con'
eststo: reg `y' c.`x'#yearN i.yearN  `c'  farrive#fcenter2  $cont_ind_base  if `con'
eststo: reg `y' c.`x'#yearN i.yearN  `c'  farrive#fcenter2  $cont_ind  if `con'
eststo: reg `y' c.`x'#yearN i.yearN   $cont  if  `con' 
eststo: reg `y' c.`x'#yearN i.yearN   $cont  country#cantnum2 if  `con' 
eststo: reg `y' c.`x'#yearN i.yearN   $cont country#cantnum2 cantnum#c.year2 if  `con' 
esttab,  keep(*.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01) 
esttab using "$text/stepwise_`y'_`x'.tex", replace $add noobs nomtitle indicate( "$indicate" "$indicate2" , l(\checkmark)) keep(*.yearN#c.`x') ///
coef(1.yearN#c.`x' "`:variable label `x'', year 1" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 


**********************************************
** Table S11: Different time span 3-5 years **
**********************************************

loc x  lnmatch  
loc y  emp  
eststo clear
forvalues i=3/4 {
eststo: reg `y' c.`x'#yearN i.yearN $cont if $restb  &  ind_cov_`i'==`i'   , cl($clust)
}
forvalues i=3/5 {
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest   & yearN<=`i'  , cl( $clust)
}
esttab,  keep(*.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01) 
esttab using "$text/timespan_`y'_`x'.tex", replace keep(*.yearN#c.`x') $add coef(1.yearN#c.`x' "`:variable label `x'', year 1" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 


**************************************
** Table S12: Heterogenous effects  **
**************************************
 
eststo clear
sum age_arrive if $rest & yearN==1, d
loc a=r(p50)
sum emp if $rest & age_arrive<=`a' & yearN==5 // 43 %
sum emp if $rest & age_arrive>`a' & yearN==5 // 34 %

loc x lnmatch 
foreach y in  emp emp90 {
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest  & woman==1 , cl($clust)
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest  & woman==0 , cl($clust)
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest  & age_arrive<=`a' , cl( $clust)
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest  & age_arrive>`a' , cl($clust)
}
esttab, keep(*.yearN#c.*) star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3)
esttab using "$text/heterogenous_gender.tex", replace $add keep(*.yearN#c.*) mtitle(Women Men Young Old Women Men Young Old) nonumb mgroups("Employed" "Employed $\geq$ 90 days" , pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
coef(1.yearN#c.`x' "`:variable label `x'', year 1" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 




**************************************************************
**** S13-S14:  Interaction effect with ethnicity/language  ***
**************************************************************

* create measure for within canton variation in 3 categories 
foreach x in   spr ethn  {
cap drop `x'_group 
g `x'_group=.
levelsof cantass, local(clist)
foreach c of local clist {
cap drop `x'_group_`c'
xtile `x'_group_`c'=share_`x' if $rest  & cantass=="`c'", nq(3) 
replace `x'_group=`x'_group_`c' if cantass=="`c'"
drop `x'_group_`c'
}
tab `x'_group
}

cap la drop groups3
la de groups3 1 low 2 medium  3 high

gl add_inter $add mlabel(Ethnicity  Language) // don't change order
foreach k in  lnmatch  {
eststo clear
foreach x in  ethn  spr   {
cap drop t
g t=`x'_group
la val t groups3
loc y emp
eststo inter_`x': reg `y' `k'   c.`k'#t i.t $cont if $rest , cl($clust)
eststo marg_`x':  reg `y'  c.`k'#t i.t $cont if $rest , cl($clust)
}
esttab inter_ethn  inter_spr using "$text/inter_`k'.tex", replace keep(`k' 2.t#* 3.t#*)  $add_inter  order(`x' 2.t 3.t )
esttab marg_ethn  marg_spr using  "$text/inter_marg_`k'.tex", replace keep(*.t#*)  $add_inter   order(1.t 2.t 3.t)
}




*********************************
**  Table S15: Wild Bootstrap  **
*********************************

cap mat drop p 
cap mat drop ci
eststo clear
loc x lnmatch
foreach y in emp emp90 {
eststo: reg `y' c.`x'#yearN i.yearN $cont if $rest , cl($clust)
mat t=r(table)
mat num=e(N)
mat p=nullmat(p)\t[1,1],t[1,2],t[1,3],t[1,4],t[1,5],num[1,1]
matlist p
boottest {1.yearN#c.`x'}{2.yearN#c.`x'}{3.yearN#c.`x'}{4.yearN#c.`x'}{5.yearN#c.`x'} , reps(1000) nograph
mat ci =nullmat(ci)\ r(CI_1) ,r(CI_2),r(CI_3),r(CI_4),r(CI_5)
matlist ci
}

mat p=p'
mat ci=ci'

quietly {
	cap log close
	set linesize 100
	log using "$text/wild_lnmatch.tex", replace text
	noisily dis    " &      Employed &   Employed $\geq$ 90 days    \\"
	noisily dis    "\midrule"
	noisily dis "\addlinespace" 
	noisily dis "ln(co-nationals), year 1   & " %5.3f p[1,1]  					"&"   %5.3f p[1,2] 					 	     							"\\"
	noisily dis "  						    & ("%5.3f ci[1,1] "," %5.3f ci[2,1] ")&(" %5.3f ci[1,2] "," %5.3f ci[2,2]  	")" 		"\\"
	noisily dis "\addlinespace" 
	noisily dis "ln(co-nationals), year 2   & "	%5.3f p[2,1] 					"&"   %5.3f p[2,2]						     								"\\"
	noisily dis "   						& ("%5.3f ci[3,1] "," %5.3f ci[4,1]	")&(" %5.3f ci[3,2] "," %5.3f ci[4,2]	")" 		"\\"
	noisily dis "\addlinespace" 
	noisily dis "ln(co-nationals), year 3   & "	%5.3f p[3,1]					"&"   %5.3f p[3,2]						     							"\\"
	noisily dis "   						& ("%5.3f ci[5,1] "," %5.3f ci[6,1]	")&(" %5.3f ci[5,2] "," %5.3f ci[6,2]	")" 	"\\"
	noisily dis "\addlinespace" 
	noisily dis "ln(co-nationals), year 4   & "	%5.3f p[4,1]					"&"	  %5.3f p[4,2]						     								"\\"
	noisily dis "   						& ("%5.3f ci[7,1] "," %5.3f ci[8,1]	")&(" %5.3f ci[7,2] "," %5.3f ci[8,2]	")"    	"\\"
	noisily dis "\addlinespace" 
	noisily dis "ln(co-nationals), year 5   & "	%5.3f p[5,1]					"&"   %5.3f p[5,2]						      								"\\"
	noisily dis "   						& ("%5.3f ci[9,1] "," %5.3f ci[10,1] ")&(" %5.3f ci[9,2] "," %5.3f ci[10,2]  ")"      "\\"
	noisily dis "\midrule"
	noisily dis "Observations       		& " p[6,1] 							"&" p[6,2]  							       "\\"
	log close
}


**************************
** Table S16:  Placebo  **
**************************

 
eststo clear
loc x lnmig
foreach y in  $ylist  {
eststo: reg `y'  c.`x'#yearN  i.yearN  $cont_placebo  if $rest    , cl($clust)
}
esttab, keep( *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

esttab using "$text/placebo.tex", replace keep(*.yearN#c.`x')  $add coef(1.yearN#c.`x' "`:variable label `x'', year 1" 2.yearN#c.`x' "`:variable label `x'', year 2" 3.yearN#c.`x' "`:variable label `x'', year 3" 4.yearN#c.`x' "`:variable label `x'', year 4" 5.yearN#c.`x' "`:variable label `x'', year 5") 


****************************************************************
****  Table S17-S18: tables for industry and share employed  ***
****************************************************************

cap drop  total
foreach x in  1 2 {
egen total=sum(emp) if $rest
preserve
keep if high`x'==1
bys industry`x': egen share =sum(emp) if $rest
replace share=share/total if $rest
format share %9.3f
keep industry`x' share
ren industry`x' ind1
ren share share1
drop if mi(share1)
duplicates drop
g n=_n
sort n
tempfile t1
save "`t1'", replace
restore

preserve
keep if high`x'==0
bys industry`x': egen share =sum(emp) if $rest
replace share=share/total if $rest
format share %9.3f
keep industry`x' share
ren industry`x' ind2
ren share share2
drop if mi(share2)
duplicates drop
g n=_n
sort n
merge 1:1 n using  "`t1'"
 
set obs `=_N+1' 
decode ind2, g(ind2_)
decode ind1, g(ind1_)

replace ind2_= "Total" if _n==_N
replace ind1_= " \hline Total" if _n==_N 
egen sum1=sum(share1)
replace share1=sum1 if _n==_N 
egen sum2=sum(share2)
replace share2=sum2 if _n==_N 
 
cd "$text"
listtex ind1_ share1  ind2_ share2 using "wc_industry_`x'.tex",replace type rstyle(tabular) head("\begin{tabular}{l*{3}{l}} \hline\hline"  "\textbf{High-skilled}&{share}&\textbf{Low-skilled}&{share} \\\\") foot("\hline\hline \end{tabular}")
restore
drop total
}




***************************************************
***************************************************
** Results, extra checks not reported in paper  ***
***************************************************
***************************************************

************************************************************
** Using current  number of co-nationals instead of t-1  ***
************************************************************

eststo clear
loc x lnmatch_current 
foreach y in $ylist   {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont  if $rest , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*very similar results


************************************************************
** Drop cases with extremely high number of conationals  ***
************************************************************

sum match if $rest & yearN==1, d // average 560

eststo clear
loc x lnmatch
foreach y in $ylist   {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont  if $rest & match<560, cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*increases precision, significant effect year 5

********************************************************
** Using all ages for co-nationals instead of 18-65  ***
********************************************************
gl rest_alt   /*age_arrive>=18 & age_arrive<=65 &*/ cantreq!=1 & getF==1  & ind_cov_5==5 & gap_day!=1

eststo clear
loc x lnmatch_all 
foreach y in $ylist   {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont  if $rest_alt  , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*very similar results, but weaker effect for employment year 4

******************************************************************
** Add restriction country of birth= country of citizenship ***
******************************************************************

gl rest_alt   age_arrive>=18 & age_arrive<=65 & cantreq!=1 & getF==1 & country== country_born  & ind_cov_5==5 & gap_day!=1

eststo clear
loc x lnmatch
foreach y in  $ylist {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont     if $rest_alt   , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*increases precision 5th year


***********************************************************
** Include individuals even if not observed all 5 years ***
***********************************************************

eststo clear
loc x lnmatch
foreach y in  $ylist {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont  if $restb  & yearN<=5 & yearS>=5  , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*very similar

**********************************************************
** drop if multiple cantons mention in assignment file ***
**********************************************************

gl rest_alt   age_arrive>=18 & age_arrive<=65 & cantreq!=1 & getF==1 & multiple_cant_tot!=1  & ind_cov_5==5 & gap_day!=1

eststo clear
loc x lnmatch
foreach y in  $ylist {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont     if $rest_alt   , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*very similar

****************************************
** include individuals who re-enter  ***
****************************************

gl rest_alt   age_arrive>=18 & age_arrive<=65 & cantreq!=1 & getF==1  & ind_cov_5==5 /*& gap_day!=1*/

eststo clear
loc x lnmatch
foreach y in  $ylist {
eststo: reg `y'  c.`x'#yearN i.yearN   $cont     if $rest_alt   , cl($clust)
}
esttab, keep(  *.yearN#c.`x') star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) 

*very similar

***********************************************
* Table S19: example of groups in Afghanistan    **
***********************************************

foreach y in 501 512 {
use "$dat/ZEMIS_start.dta", clear
g individual=1 if yearN==yearS
keep if country==`y'
keep if (getF==1|getBC==1)
keep if stus_flag==40 | (stat_end<= 510900 & !mi(stat_end))  // remove if left the country
keep ethn_cd spr_cd individual
keep if individual==1

foreach x in ethn spr  {
preserve
keep `x'_cd individual
egen total=sum(individual)
bys `x'_cd: egen share_`x' =sum(individual)
replace share_`x'=share_`x'/total
drop total individual
duplicates drop
format share_`x' %9.3f
sort `x'_cd
g n=_n
tempfile b`x'
save "`b`x''", replace
restore
}

use "`bethn'", clear
merge 1:1 n using "`bspr'"
drop _merge
drop n

if `y'==501 listtex ethn_cd share_ethn spr_cd share_spr   using "$text/example_afghanistan.tex",replace type rstyle(tabular) head("\begin{tabular}{l*{4}{l}} \hline\hline"  "\textbf{Ethnicity}&Share&\textbf{Language}&Share\\\\") foot("\hline\hline \end{tabular}")
if `y'==512 listtex ethn_cd share_ethn spr_cd share_spr  using "$text/example_iraq.tex",replace type rstyle(tabular) head("\begin{tabular}{l*{4}{l}} \hline\hline"  "\textbf{Ethnicity}&Share&\textbf{Language}&Share\\\\") foot("\hline\hline \end{tabular}")
}


